package lin.tb.com.commonadapter;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by zhenggl on 2015/8/6.
 */
public class CommonViewHolder {

    private final SparseArray<View> mViews;
    private int mPosition;
    private View mConvertView;
    private CommonViewHolder holder;

    public CommonViewHolder(Context context, ViewGroup parent, int layout, int position){
        mViews = new SparseArray<>();
        mConvertView = LayoutInflater.from(context).inflate(layout, parent, false);
        mConvertView.setTag(this);
    }

    public static CommonViewHolder get(Context context, View convertView, int layout, ViewGroup parent, int position){
        if(convertView == null){
            return new CommonViewHolder(context, parent, layout, position);
        }
        return (CommonViewHolder)convertView.getTag();
    }

    public <T extends View> T getView(int viewId){
        View view = mViews.get(viewId);
        if(null == view){
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T)view;
    }

    public TextView getTextView(int viewId){
        View view = getView(viewId);
        if(view instanceof TextView){
            return (TextView)view;
        }
        return null;
    }

    public View getConvertView(){
        return mConvertView;
    }
}
